/**
 * 检测是否在微前端环境中运行
 * @returns {boolean} 如果在微前端环境中返回true，否则返回false
 */
export const isMicroApp = (): boolean => {
  // 检查微前端环境标识
  const hasMicroAppEnv = !!(window as any).__MICRO_APP_ENVIRONMENT__

  // 检查是否在iframe中（micro-app使用iframe模式）
  const isInIframe = window.parent !== window

  // 检查URL参数中是否包含主应用传递的标识
  const urlParams = new URLSearchParams(window.location.search)
  const fromMainApp = urlParams.get('from') === 'main-app'

  // 检查微前端相关的全局对象
  const hasMicroAppGlobal = !!(window as any).microApp

  // 只要满足任一条件就认为是在微前端环境中
  const result = hasMicroAppEnv || isInIframe || fromMainApp || hasMicroAppGlobal

  console.log('微前端环境检测结果:', {
    hasMicroAppEnv,
    isInIframe,
    fromMainApp,
    hasMicroAppGlobal,
    isMicroApp: result,
  })

  return result
}

/**
 * 获取应用运行模式的描述
 * @returns {string} 运行模式描述
 */
export const getAppMode = (): string => {
  return isMicroApp() ? '微前端模式' : '独立运行模式'
}
